from PySide2 import QtCore, QtWidgets, QtGui
import sys
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 800)         # 设置显示窗口的大小（也就是打开后的初始窗口大小）
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.treeView = QtWidgets.QTreeView(self.centralwidget)
        self.treeView.setGeometry(QtCore.QRect(0, 0, 800, 800))     # 设置treeview的显示大小
        # 设置垂直滚动条为按需显示
        self.treeView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        # 设置水平滚动条为按需显示
        self.treeView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        # 设置双击或者按下回车时，使树节点可编辑
        self.treeView.setEditTriggers(QtWidgets.QAbstractItemView.DoubleClicked
                                      | QtWidgets.QAbstractItemView.EditKeyPressed)
        # 设置树节点为单选
        self.treeView.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
        # 设置节点时为整行选中
        self.treeView.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
        self.treeView.setAutoExpandDelay(-1)
        self.treeView.setItemsExpandable(True)
        self.treeView.setSortingEnabled(True)
        self.treeView.setWordWrap(True)
        self.treeView.setHeaderHidden(False)
        self.treeView.setExpandsOnDoubleClick(True)     # 设置双击可以展开节点
        self.treeView.setObjectName("treeView")

        self.treeView.header().setVisible(True)         # 设置显示头部
        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        model = QtWidgets.QDirModel()      # 创建存储文件系统的模型
        self.treeView.setModel(model)      # 为树控件设置数据模型

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())


